package org.xzx.shop_api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xzx.shop_api.entity.Address;
import org.xzx.shop_api.service.AddressService;
import org.xzx.shop_api.service.AreasService;
import org.xzx.shop_api.service.UserService;
import org.xzx.shop_api.utils.JsonResult;

import java.util.List;

@RestController
@RequestMapping("api/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;
    @Autowired
    private AreasService areasService;

    //收货地址添加接口
    @PostMapping("add")
    public JsonResult add(@RequestBody Address address) {
        return addressService.addAddress(address);
    }

    //收获地址列表接口
    @GetMapping("list")
    public JsonResult listAddress(@RequestParam int userId) {
        return addressService.listAllAddress(userId);
    }

    //收货地址详情接口
    @GetMapping("detaile")
    public JsonResult detailAddress(@RequestParam int id) {
        return addressService.getAddressDetail(id);
    }
    //收获地址编辑接口
    @PostMapping("update")
    public JsonResult updateAddress(@RequestBody Address address) {
        return addressService.updateAddress(address);
    }
    //收获地址删除接口
    @GetMapping("delete")
    public JsonResult deleteAddress(@RequestParam int id,@RequestParam int userId) {
        return addressService.deleteAddress(id,userId);
    }

    //省市区三级联动查询接口
    @GetMapping("areas/province")
    public JsonResult getProvinces() {
        return JsonResult.success("查询成功", areasService.getProvinces());
    }
    @GetMapping("areas/city")
    public JsonResult getCities(@RequestParam int provinceId) {
        return JsonResult.success("查询成功", areasService.getCitiesByProvinceld(provinceId));
    }

    @GetMapping("area/area")
    public JsonResult getAreas(@RequestParam int cityId) {
        return JsonResult.success("查询成功", areasService.getAreasByCityId(cityId));
    }
    //一对多关联查出会员名及收获地址列表
    @GetMapping("with-user")
    public JsonResult getUsers(@RequestParam int userId) {
        List<Address> address = addressService.getAddressesWithUser(userId);
        return JsonResult.success("查询成功",address);
    }
}



























